package org.csu.mybigpro.controller;

import org.csu.mybigpro.domain.User;
import org.csu.mybigpro.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    public List<User> getAllTeachers() {
        return teacherService.getAllTeachers();
    }

    @GetMapping("/{id}")
    public User getTeacherById(@PathVariable Long id) {
        return teacherService.getTeacherById(id);
    }

    @GetMapping("/name/{name}")
    public List<User> getTeacherByName(@PathVariable String name) {
        return teacherService.getTeacherByName(name);
    }

    @PostMapping("/add")
    public String addTeacher(@RequestBody User user) {
        return teacherService.addTeacher(user) ? "添加成功" : "添加失败";
    }

    @PutMapping("/update")
    public String updateTeacher(@RequestBody User user) {
        return teacherService.updateTeacher(user) ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteTeacher(@PathVariable Long id) {
        return teacherService.deleteTeacher(id) ? "删除成功" : "删除失败";
    }
}
